﻿using System;
using System.Linq;
using System.Web;
using Thuoc24h.Business;
using Thuoc24h.Common;

namespace Thuoc24h.Home.Pages
{
    public partial class NewsDetail : BasePage.BasePages
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = Page.RouteData.Values["id"].ToInt32Return0();
            if (!Page.IsPostBack)
            {
                var news = ReviewRepository.NewsById(id);
                if (news != null)
                {
                    lbHeader.Visible = false;
                    ltrDetail.Text = news.Detail;
                    ltrDescription.Text = news.Description;
                    Page.Title = ltrTitle.Text = news.Title;
                    ltrImage.Text = "<img width=\"150\" class=\"LSNewsDetailThumb\" src=" + news.ThumbImage + ">";

                    if (ReviewRepository.GetNewsReferencesCount(id) > 0)
                    {
                        lbHeader.Visible = true;
                        rptNewsRefer.DataSource = ReviewRepository.GetNewsReferencesDetail(id);
                        rptNewsRefer.DataBind();
                    }
                    UpdateViewCount(id);
                }
            }
        }
        private void UpdateViewCount(int id)
        {


            var view = "NewsViewCount" + id;
            var cookied = Request.Cookies[view];
            if (cookied == null)
            {
                var news = context.News.SingleOrDefault(item => item.Id == id);

                if (news != null)
                {
                    news.ViewCount += 1;
                    news.ViewCount += 1;
                    context.SaveChanges();
                }

                var cookie = new HttpCookie(view) { Value = view, Expires = DateTime.Now + new TimeSpan(0, 0, 30, 0) };
                Response.Cookies.Add(cookie);
            }



        }
    }
}